<?php

/**
 * Item form.
 *
 * @package    form
 * @subpackage Item
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 6174 2007-11-27 06:22:40Z fabien $
 */
class ItemForm extends BaseItemForm
{
  public function configure()
  {
  	parent::configure();

      unset($this['st_item'], $this['id_usuario']);

      $this->widgetSchema['ds_item']         = new sfWidgetFormTextareaTinyMCE(array(
                                                                                  'width'  => 700,
                                                                                  'height' => 200,
                                                                                  'config' => 'theme_advanced_disable: "anchor,image,cleanup,help"',
                                                                                ));

      $this->widgetSchema['ds_resumo']         = new sfWidgetFormTextareaTinyMCE(array(
                                                                                  'width'  => 700,
                                                                                  'height' => 100,
                                                                                  'config' => 'theme_advanced_disable: "anchor,image,cleanup,help"',
                                                                                ));


      $this->widgetSchema['id_tipo']        = new sfWidgetFormDoctrineChoice(array('model' => 'Tipo', 'add_empty' => "-- Selecione --"));

      /*
       * Formulário embutido de mídia
       */
      $form = new MidiaItemForm(null, array(
				    'item' => $this->getObject(),//nome do objeto
				    'size'    => 10//quantidade de
				  ));
      
      $this->embedForm('midia', $form);

      $this->validatorSchema['st_item'] = new sfValidatorBoolean(array('required' => false));
      $this->validatorSchema['destaque'] = new sfValidatorBoolean(array('required' => false));

      $this->widgetSchema->setLabels(array(
                                      'nm_item'         => 'Nome',
                                      'ds_item'         => 'Descrição',
                                      'pb_item'         => 'Publicar?',
                                      'vl_item'         => 'Valor',
                                      'qtd_item'        => 'Quantidade',
                                      'ds_tags'         => 'Tags',
                                      'id_tipo'         => 'Tipo',
                                      'file_midia'      => 'Arquivo',
                                      'ds_midia'        => 'Descrição',
                                      'ds_resumo'       => 'Resumo',
                                      'destaque'        => 'Destacar?'
                                    ));


      $this->validatorSchema->setOption('allow_extra_fields', true);
  }

  public function saveEmbeddedForms($con = null, $forms = null)
    {
      if (null === $forms)
      {
        $midia = $this->getValue('midia');

        $forms = $this->embeddedForms;
        foreach ($this->embeddedForms['midia'] as $name => $form)
        {
          if (!isset($midia[$name]))
            unset($forms['midia'][$name]);
        }
      }
      parent::saveEmbeddedForms($con, $forms);

      //Criando as Thumbs
      foreach($this->getObject()->getMidia() as $midia)
      {
           $thumb   = new Midia();
           $thumb->createThumb($midia);
      }

      return;
    }
}